/*
 * Tic-Tac-Toe - Turn-based strategy game
 * Copyright 2008, 2009 Shayne Riley and Paul Maseberg
 *
 * Tic-Tac-Toe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tic-Tac-Toe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tic-Tac-Toe.  If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner at gmail dot com>
 */

#include "Graphics.h"
#include "Audio.h"
#include "GameBoard.h"
#include "BoardDisplay.h"
#include "TitleScreen.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#endif

void boardCompletion(Graphics *gfx, Audio *aud, BoardDisplay *display,
        GameBoard::Difficulty difficulty)
{
    static bool completing = false; // TODO DUMB IMPL. HURTS BRAIN TO LOOK.
    static Uint32 startCompletionMillis = gfx->frameMillis;

    GameBoard board = display->getBoard();
    if (board.isGameOver())
    {
        // draw the semi-transparent backing.
        gfx->setColor(1.0f, 1.0f, 1.0f, 0.7f);
        gfx->drawRectFill(0, 0, 640, 480);

        // draw the text.
        gfx->setColor(0.1f, 0.6f, 0.1f, 1);

        if (board.winner() > 0)
        {
            gfx->drawTexture(&gfx->textureYou, 142, 176);
            gfx->drawTexture(&gfx->textureWin, 320, 176);
            if (!completing)
            {
                aud->play(Audio::SONG_WIN);
            }
        }
        else if (board.winner() < 0)
        {
            gfx->drawTexture(&gfx->textureYou, 142, 176);
            gfx->drawTexture(&gfx->textureLose, 320, 176);
            if (!completing)
            {
                aud->play(Audio::SONG_LOSE);
            }
        }
        else
        {
            gfx->drawTexture(&gfx->textureCat, 251, 176);
            if (!completing)
            {
                aud->play(Audio::SONG_CAT);
            }
        }

        if (!completing)
        {
            startCompletionMillis = gfx->frameMillis;
        }

        completing = true;
    }

    if (completing && 2000 <= gfx->frameMillis- startCompletionMillis)
    {
        completing = false;
        GameBoard freshBoard = GameBoard(difficulty);
        display->updateBoard(freshBoard);
    }
}

/**
 * loops while the game is active.
 */
void gameLoop(Graphics *gfx, Audio *aud)
{
    bool shouldLoop = true;

    TitleScreen ts = TitleScreen(gfx, aud);
    ts.perform();

    GameBoard::Difficulty difficulty = ts.getSettingDifficulty();
    GameBoard startBoard = GameBoard(difficulty);

    BoardDisplay display = BoardDisplay(gfx, aud, 128, 48, 384, 384);
//    BoardDisplay display = BoardDisplay(gfx, aud, 0, 0, 640, 480);
    display.updateBoard(startBoard);

    while (shouldLoop)
    {
        shouldLoop = gfx->drawGame();

        display.update();
        boardCompletion(gfx, aud, &display, difficulty);

        gfx->endFrame();
    }
}

// Returns 0 on success and -1 on failure.
int start()
{
    Graphics gfx;
    Audio aud;

    if (!gfx.operational)
    {
        return -1;
    }

    gameLoop(&gfx, &aud);

    gfx.destroy();

	return 0;
}

int main(int argc, char **argv)
{
	start();

    return 0;
}

#ifdef _WIN32
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{
	start();

	return 0;
}
#endif
